package com.hongxun.budget.ui.dialog;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.greenentity.SysUserEntity;
import com.hongxun.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 多选dialog
 */

public class ListCheckDialog<T> extends BaseCenterFragmentDialog {

    @BindView(R.id.title)
    TextView titleTv;
    @BindView(R.id.list)
    RecyclerView list;
    String title;
    ResponseListener listener;
    List<T> datas = new ArrayList<>();//源数据
    SuperAdapter<T> superAdapter;
    @BindView(R.id.cancel)
    Button cancel;
    private Map<Integer, Boolean> map = new HashMap<>();//记录被勾选项，防止错乱

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_list;
    }

    @Override
    protected void initViews() {
        cancel.setText("确定");
        if (!TextUtils.isEmpty(title)) titleTv.setText(title);
        list.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(getActivity()));
        list.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        list.setAdapter(superAdapter = new SuperAdapter<T>(R.layout.content_dialog_checklist) {
            @Override
            public void bindView(ViewHolder viewHolder, final T t, final int i) {
                //TODO 根据传入参数设置值,如下
                TextView textView = viewHolder.getView(R.id.content);
                if (t instanceof String) {
                    textView.setText((String) t);
                } else if (t instanceof Integer) {
                    textView.setText((Integer) t + "");
                } else if (t instanceof SysUserEntity) {
                    textView.setText(((SysUserEntity) t).getUsername() + "(" + ((SysUserEntity) t).getDeptName() + ")");
                }
                CheckBox checkBox = viewHolder.getView(R.id.checkBox);
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (b == true) map.put(i, true);
                        else map.remove(i);

                    }
                });
                if (map != null && map.containsKey(i)) {
                    checkBox.setChecked(true);
                } else {
                    checkBox.setChecked(false);
                }
            }
        });
        superAdapter.setData(datas);
    }

    public ListCheckDialog setTitle(String title) {
        this.title = title;
        if (titleTv != null) titleTv.setText(title);
        return this;
    }

    public ListCheckDialog setDatas(List<T> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        if (superAdapter != null) superAdapter.notifyDataSetChanged();
        return this;
    }

    public ListCheckDialog setChecked(Map<Integer, Boolean> map) {
        this.map = map;
        return this;
    }

    public ListCheckDialog setListener(ResponseListener listener) {
        this.listener = listener;
        return this;
    }

    @OnClick(R.id.cancel)
    public void onViewClicked() {
        List<T> resultDatas = new ArrayList<>();//选择的结果
        for (Integer integer : map.keySet()) {
            resultDatas.add(datas.get(integer));
        }
        listener.response(resultDatas, map);
        dismiss();
    }
}
